package Response;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.ResultSet;

public class Login extends HttpServlet {
	PreparedStatement ps=null;
	PreparedStatement pstmt=null;
	Connection con=null;
 	ResultSet rs=null;
public void init() throws ServletException 
{
	try 
	{

		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql://localhost:3306/websurvey";
		con=DriverManager.getConnection(url, "root", "mysql");
		System.out.println("connection established");
		
	} 
	catch (ClassNotFoundException e) 
	{
		e.printStackTrace();
	} catch (SQLException e) 
	{
		e.printStackTrace();
	}	
}
	

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		try {
			pstmt=con.prepareStatement("select * from login where email = ? and password= ?");

			String unm=request.getParameter("unm");
			String pwd=request.getParameter("pass");

			
			   pstmt.setString(1,unm);
			   pstmt.setString(2, pwd);
			   
			   boolean flag=false;
			   
			   rs=(ResultSet) pstmt.executeQuery();
			   
			   
			  
			   while(rs.next())
			   {
				   if(rs.getString(1).equals(unm) && rs.getString(2).equals(pwd))
				   {
					   out.println("Login successful");
					   flag=true;
					   break;
				   }
			   }
			   if(!flag)
			   {
				   out.println("Login fail");
			   }
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}

	public void destroy() 
	{
		super.destroy(); 
	}
	

}
